class Solution {
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        
        //找到最后一个的数的位置
        while (cur < arr.length) {
            if (arr[cur] != 0) {
                dest++;
            } else {
                dest += 2;
            }
            if (dest >= arr.length - 1) {
                break;
            }
            cur++;
        }

        //处理临界问题
        if (dest == arr.length) {
            arr[dest-1] = 0;
            cur--;
            dest -= 2;
        }

        //从后往前复写
        while (cur >= 0) {
            if (arr[cur] != 0) {
                arr[dest--] = arr[cur--];
            } else {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }
}